package com.google.googlemarket.adapter;

import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.googlemarket.MyApp;
import com.google.googlemarket.R;
import com.google.googlemarket.bean.CategoryInfo;
import com.google.googlemarket.http.BitmapHelp;
import com.google.googlemarket.http.Urls;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/9/26.
 */
public class CategoryAdapter extends MyBaseAdapter<Object> {

    private static final int TYPE_TITLE=0;
    private static final int TYPE_CONTENT=1;

    public CategoryAdapter(ArrayList<Object> datas) {
        super(datas);
    }

    @Override
    public int getItemViewType(int position) {
        return (datas.get(position) instanceof String) ? TYPE_TITLE : TYPE_CONTENT;
    }

    public static int getTypeContent() {
        return 2;
    }

    @Override
    protected int getItemLayoutId(int position) {
        return getItemViewType(position) == TYPE_TITLE ? R.layout.item_category_title:R.layout.item_category;
    }

    @Override
    protected Object createViewHolder(View convertView, int position) {
      ViewHolder viewHolder = new ViewHolder();
        if (getItemViewType(position)==TYPE_TITLE){
            viewHolder.textview = (TextView) convertView;
        }else{
            viewHolder.ll_1 = (LinearLayout) ((LinearLayout) convertView).getChildAt(0);
            viewHolder.ll_2= (LinearLayout) ((LinearLayout) convertView).getChildAt(1);
            viewHolder.ll_3 = (LinearLayout) ((LinearLayout) convertView).getChildAt(2);
        }
        return viewHolder;
    }

    @Override
    protected void showData(int position, Object holder, Object data) {
        ViewHolder viewholder = (ViewHolder) holder;
        if (data instanceof String){
            viewholder.textview.setText((String) data);
        }else{
            CategoryInfo.CategoryItem cateGory = (CategoryInfo.CategoryItem) data;
            BitmapHelp.getBitmapUtils(MyApp.getmContext()).display(viewholder.ll_1.getChildAt(0), Urls.IMAGE+"?name="+cateGory.url1);
            BitmapHelp.getBitmapUtils(MyApp.getmContext()).display(viewholder.ll_2.getChildAt(0), Urls.IMAGE+"?name="+cateGory.url2);
            BitmapHelp.getBitmapUtils(MyApp.getmContext()).display(viewholder.ll_3.getChildAt(0), Urls.IMAGE+"?name="+cateGory.url3);

            ((TextView)viewholder.ll_1.getChildAt(1)).setText(cateGory.name1);
            ((TextView)viewholder.ll_2.getChildAt(1)).setText(cateGory.name2);
            ((TextView)viewholder.ll_3.getChildAt(1)).setText(cateGory.name3);

            viewholder.ll_2.setVisibility(TextUtils.isEmpty(cateGory.url2) ? View.INVISIBLE : View.VISIBLE);
            viewholder.ll_3.setVisibility(TextUtils.isEmpty(cateGory.url3) ? View.INVISIBLE : View.VISIBLE);

            viewholder.ll_1.setOnClickListener(mOnClickListener);
            viewholder.ll_2.setOnClickListener(mOnClickListener);
            viewholder.ll_3.setOnClickListener(mOnClickListener);
        }

    }

    private View.OnClickListener mOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            LinearLayout linearLayout = (LinearLayout) view;
            String text = ((TextView)linearLayout.getChildAt(1)).getText().toString();
            Toast.makeText(MyApp.getmContext(),text,Toast.LENGTH_LONG).show();
        }
    };

    private class ViewHolder {
        TextView textview;
        LinearLayout ll_1;
        LinearLayout ll_2;
        LinearLayout ll_3;
    }
}
